package com.zxw.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zxw.common.entity.user.UserEntity;
import com.zxw.common.entity.user.UserListQuery;
import com.zxw.common.entity.user.UserQuery;
import com.zxw.common.entity.user.UserVO;
import com.zxw.user.mapper.UserMapper;
import com.zxw.user.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户信息表 服务实现类
 * </p>
 *
 * @author zhanxuewei
 * @since 2022-09-08
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, UserEntity> implements UserService {

    /**
     * 添加
     *
     * @param userQuery UserQuery
     * @return 是否成功 true|false
     */
    @Override
    public boolean save(UserQuery userQuery) {
        UserEntity userEntity = new UserEntity();
        BeanUtils.copyProperties(userQuery, userEntity);
        userEntity.setStatus("1");
        userEntity.setEnabled(true);
        userEntity.setPassword(new BCryptPasswordEncoder().encode(userQuery.getPassword()));
        return super.save(userEntity);
    }

    /**
     * 批量添加
     *
     * @param list List<UserQuery>
     * @return 是否成功 true|false
     */
    @Override
    public boolean saveBatch(List<UserQuery> list) {
        List<UserEntity> collect = list.stream().map(item -> {
            UserEntity userEntity = new UserEntity();
            BeanUtils.copyProperties(item, userEntity);
            userEntity.setStatus("1");
            userEntity.setEnabled(true);
            userEntity.setPassword(new BCryptPasswordEncoder().encode(item.getPassword()));
            return userEntity;
        }).collect(Collectors.toList());
        return super.saveBatch(collect);
    }

    /**
     * 根据ID集合批量删除
     *
     * @param ids id集合
     * @return 是否成功 true|false
     */
    @Override
    public boolean removeByIds(List<String> ids) {
        return super.removeByIds(ids);
    }

    /**
     * 根据ID修改
     *
     * @param userQuery UserQuery
     * @return 是否成功 true|false
     */
    @Override
    public boolean updateById(UserQuery userQuery) {
        String new_password = userQuery.getPassword();
        UserEntity userEntity = new UserEntity();
        BeanUtils.copyProperties(userQuery, userEntity);
        if(StringUtils.isNotBlank(new_password)){
            userEntity.setPassword(new BCryptPasswordEncoder().encode(new_password));
        }
        return super.updateById(userEntity);
    }

    /**
     * 根据ID查询数据
     *
     * @param id ID
     * @return UserDO
     */
    @Override
    public UserEntity getById(String id) {
        return super.getById(id);
    }

    /**
     * 条件分页查询
     *
     * @param userListQuery UserListQuery
     * @return IPage<UserDO>
     */
    @Override
    public IPage<UserEntity> list(UserListQuery userListQuery) {
        IPage<UserEntity> pageInfo = new Page<>(userListQuery.getCurrent(), userListQuery.getSize());
        QueryWrapper<UserEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(UserEntity::getStatus, 1)
                .orderByDesc(UserEntity::getCreateTime);
        return super.page(pageInfo, queryWrapper);
    }

    @Override
    public UserVO queryByUsername(String username) {
        QueryWrapper<UserEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(UserEntity::getUserName, username);
        UserEntity userEntity = super.getOne(queryWrapper);
        if (Objects.nonNull(userEntity)) {
            UserVO userVO = new UserVO();
            BeanUtils.copyProperties(userEntity, userVO);
            return userVO;
        }
        return null;

    }

    @Override
    public IPage<UserVO> pageList(UserListQuery userListQuery) {

        IPage<UserEntity> pageInfo = new Page<>(userListQuery.getCurrent(), userListQuery.getSize());
        QueryWrapper<UserEntity> queryWrapper = new QueryWrapper<>();
        LambdaQueryWrapper<UserEntity> lambda = queryWrapper.lambda();
        if (StringUtils.isNotBlank(userListQuery.getUserName())) {
            lambda.like(UserEntity::getUserName, userListQuery.getUserName());
        }

        if (StringUtils.isNotBlank(userListQuery.getNickName())) {
            lambda.like(UserEntity::getNickName, userListQuery.getNickName());
        }

        if (StringUtils.isNotBlank(userListQuery.getPhone())) {
            lambda.like(UserEntity::getPhone, userListQuery.getPhone());
        }

        if (StringUtils.isNotBlank(userListQuery.getEmail())) {
            lambda.like(UserEntity::getEmail, userListQuery.getEmail());
        }

        if (Objects.nonNull(userListQuery.getEnabled())) {
            lambda.eq(UserEntity::getEnabled, userListQuery.getEnabled());
        }
        if (Objects.nonNull(userListQuery.getIsActive())) {
            lambda.eq(UserEntity::getIsActive, userListQuery.getIsActive());
        }

        IPage<UserEntity> page = super.page(pageInfo, queryWrapper);

        return page.convert(userEntity -> {
            UserVO userVO = new UserVO();
            BeanUtils.copyProperties(userEntity, userVO);
            return userVO;
        });
    }

}
